package com.java.onlineshop.impl;

import com.java.onlineshop.dao.IProductDao;
import com.java.onlineshop.pojo.Category;
import com.java.onlineshop.pojo.Product;
import com.java.onlineshop.service.ICategoryService;
import com.java.onlineshop.service.IProductService;
import com.java.onlineshop.service.impl.CategoryServiceImpl;
import com.java.onlineshop.service.impl.ProductServiceImpl;
import com.java.onlineshop.util.JdbcUtil;
import org.junit.jupiter.api.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: onlineshop
 * @description: IProductDao实现类
 * @author: ShuangChenYue
 * @create: 2021-06-05 13:52
 **/
public class ProductDaoImpl implements IProductDao
{

    @Override
    public List<Product> findAllProduct()
    {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        Product product = null;
        Category category = null;
        List<Product> productList = new ArrayList<Product>();
        ICategoryService categoryService = new CategoryServiceImpl();
        try
        {
            //1.获取连接
            connection = JdbcUtil.getConnection();
            //2.关闭自动提交，设置手动提交
            connection.setAutoCommit(false);
            //3.预编译sql语句
            String sql = "select * from product";
            preparedStatement = connection.prepareStatement(sql);
            //4.执行操作并返回结果集
            resultSet = preparedStatement.executeQuery();
            //5.处理结果集
            while(resultSet.next())
            {
                //6.若结果集中有数据，则依次取出并存进product对象中
                product = new Product();
                product.setId(resultSet.getInt("id"));
                product.setName(resultSet.getString("name"));
                product.setCompany(resultSet.getString("company"));
                product.setPages(resultSet.getInt("pages"));
                product.setImages(resultSet.getString("images"));
                product.setDetailimages(resultSet.getString("detailimages"));
                product.setCartimages(resultSet.getString("cartimages"));
                product.setBaseprice(resultSet.getDouble("baseprice"));
                product.setDescriptions(resultSet.getString("descriptions"));
                category = categoryService.findById(resultSet.getInt("categoryid"));
                product.setCategory(category);
                //都设置好后将product对象放进集合中
                productList.add(product);
            }
            connection.commit();
        }catch (Exception e)
        {
            try
            {
                connection.rollback();
            } catch (SQLException throwables)
            {
                throwables.printStackTrace();
            }
            e.printStackTrace();
        }finally
        {
            JdbcUtil.relase(resultSet,preparedStatement,connection);
        }
        return productList;
    }

    @Override
    public Product findProductById(int id)
    {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        Product product = null;
        ICategoryService categoryService = new CategoryServiceImpl();
        try
        {
            //1.获取连接
            connection = JdbcUtil.getConnection();
            //2.设置手动提交
            connection.setAutoCommit(false);
            //3.预编译sql语句
            String sql = "select * from product where id = ?";
            preparedStatement = connection.prepareStatement(sql);
            //4.填充占位符
            preparedStatement.setInt(1,id);
            //5.执行操作并返回结果集
            resultSet = preparedStatement.executeQuery();
            //6.处理结果集
            while (resultSet.next())
            {
                product = new Product();
                //7.依次将结果集中的东西存进product对象中
                product.setId(resultSet.getInt("id"));
                product.setName(resultSet.getString("name"));
                product.setCompany(resultSet.getString("company"));
                product.setPages(resultSet.getInt("pages"));
                product.setImages(resultSet.getString("images"));
                product.setDetailimages(resultSet.getString("detailimages"));
                product.setCartimages(resultSet.getString("cartimages"));
                product.setBaseprice(resultSet.getDouble("baseprice"));
                product.setDescriptions(resultSet.getString("descriptions"));
                int categoryid = resultSet.getInt("categoryid");
                Category category = categoryService.findById(categoryid);
                product.setCategory(category);
            }
            connection.commit();
        }catch (Exception e)
        {
            try
            {
                connection.rollback();
            } catch (SQLException throwables)
            {
                throwables.printStackTrace();
            }
            e.printStackTrace();
        }finally
        {
            JdbcUtil.relase(resultSet,preparedStatement,connection);
        }

        return product;
    }

    @Override
    public List<Product> findProductByLikeName(String name)
    {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        Product product = null;
        ICategoryService categoryService = new CategoryServiceImpl();
        List<Product> productList = new ArrayList<Product>();
        try
        {
            //1.获取连接
            connection = JdbcUtil.getConnection();
            //2.设置手动提交
            connection.setAutoCommit(false);
            //3.预编译sql语句
            String sql = "select * from product where name like ?";
            preparedStatement = connection.prepareStatement(sql);
            //4.填充占位符
            preparedStatement.setString(1,"%"+name+"%");
            //6.执行操作并返回结果集
            resultSet = preparedStatement.executeQuery();
            //7.处理结果集
            while (resultSet.next())
            {
                //8.若结果集中有东西，则依次取出并存进product对象中
                product = new Product();
                product.setId(resultSet.getInt("id"));
                product.setName(resultSet.getString("name"));
                product.setCompany(resultSet.getString("company"));
                product.setPages(resultSet.getInt("pages"));
                product.setImages(resultSet.getString("images"));
                product.setDetailimages(resultSet.getString("detailimages"));
                product.setCartimages(resultSet.getString("cartimages"));
                product.setBaseprice(resultSet.getDouble("baseprice"));
                product.setDescriptions(resultSet.getString("descriptions"));
                int categoryid = resultSet.getInt("categoryid");
                Category category = categoryService.findById(categoryid);
                product.setCategory(category);
                productList.add(product);
            }
            //9.提交
            connection.commit();
        }catch (Exception e)
        {
            //10数据回滚
            try
            {
                connection.rollback();
            } catch (SQLException throwables)
            {
                throwables.printStackTrace();
            }
            e.printStackTrace();
        }finally
        {
            JdbcUtil.relase(resultSet,preparedStatement,connection);
        }
        return productList;
    }

    @Override
    public List<Product> findProductByCategoryId(int id)
    {
        /*方法目的：
        *   通过传入的id找到所有属于该分类的商品并返回为一个集合
        */
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        Product product = null;
        List<Product> productList = new ArrayList<Product>();
        ICategoryService categoryService = new CategoryServiceImpl();
        try
        {
            connection = JdbcUtil.getConnection();
            connection.setAutoCommit(false);
            String sql = "select * from product where categoryid = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,id);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next())
            {
                product = new Product();
                product.setId(resultSet.getInt("id"));
                product.setName(resultSet.getString("name"));
                product.setCompany(resultSet.getString("company"));
                product.setPages(resultSet.getInt("pages"));
                product.setImages(resultSet.getString("images"));
                product.setDetailimages(resultSet.getString("detailimages"));
                product.setCartimages(resultSet.getString("cartimages"));
                product.setBaseprice(resultSet.getDouble("baseprice"));
                product.setDescriptions(resultSet.getString("descriptions"));
                int categoryid = resultSet.getInt("categoryid");
                Category category = categoryService.findById(categoryid);
                product.setCategory(category);
                productList.add(product);
            }
        }catch (Exception e)
        {

        }finally
        {
            JdbcUtil.relase(resultSet,preparedStatement,connection);
        }
        return productList;
    }
}
